package com.app.service.impl;

import com.app.mapper.jiuquan.FileMapper;
import com.app.model.FileModel;
import com.app.service.FileService;
import com.app.util.CommonHelp;
import com.app.util.CommonUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, FileModel> implements FileService {
    @Autowired
    private FileMapper fileMapper;
    //将图片或者视频路径列表转化为ID列表
    @Override
    public StringBuilder uriListToIdList(StringBuilder idList, List<String> urlList) {
        int count=0;
        for (String url : urlList) {

            //获取文件名称fileName
            String[] split = url.split("/");
            String fileName = split[split.length - 1];
            //获取URL后缀名urlSuffix
            int lastIndexOf = url.lastIndexOf(".");
            String urlSuffix = url.substring(lastIndexOf + 1);

            FileModel fileModel = new FileModel();
            fileModel.setName(fileName);
            fileModel.setUrl(url);
            Integer fileType = CommonUtil.getFileType(urlSuffix);
            fileModel.setFileType(fileType);

            fileModel.setCreateTime(CommonHelp.getNowTimestamp());
            fileMapper.insert(fileModel);
            if (count == urlList.size()-1) {
                idList.append(fileModel.getId());
            } else {
                idList.append(fileModel.getId()).append(",");
            }
            count++;
        }
        return idList;
    }
}
